<template>
  <div class="article-container">
    <!-- 卡片布局 -->
    <el-card class="box-card">
      <!-- 头部- div作为header的具名插槽插入el-card组件内-顶部一条 -->
      <div slot="header" class="clearfix">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 卡片布局-表单 -->
      <el-form ref="form" :model="form" label-width="80px">
        <!-- 第一行 -->
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 第二行 -->
        <el-form-item label="频道">
            <el-select v-model="form.channel_id" placeholder="请选择频道">
              <el-option label="全部"  :value="null"></el-option>
              <el-option v-for="item in channelList" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <!-- 第三行 -->
        <el-form-item label="日期">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="开始日期"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.begin_pubdate"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">----</el-col>
          <el-col :span="11">
            <el-date-picker
              placeholder="结束时间"
              style="width: 100%"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              v-model="form.end_pubdate"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchFn" :loading='load'>查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 查询后的数据列表 -->
    <el-card style="margin-top:20px">
      <div slot="header" class="clearfix">根据筛选条件查询到{{ total }}条数据：</div>
      <!-- data表格数据-数组套对象 -->
      <el-table :data="tableData" style="width: 100%" v-loading='load'>
        <!--  el-table-column 列标题 prop对应对象的key-->
        <el-table-column prop="cover" label="封面" width="180">
<!-- 不能显示prop属性内容, 就用template表格单元格内加载自定义内容 -->
         <template v-slot="scope">
           <!-- 图片默认用第0个(lazy懒加载-滑动到那里再加载) -->
           <el-image
             :src="scope.row.cover.images[0]"
             style="width: 150px; height: 100px"
             lazy
           >
             <div slot="placeholder" class="image-slot">
               加载中<span class="dot">...</span>
             </div>
           </el-image>
         </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template v-slot="scope">
            <el-tag v-if="scope.row.status === 0">草稿</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="info"
            >待审核</el-tag
          >
            <el-tag v-else-if="scope.row.status === 2" type="success"
            >审核通过</el-tag
          >
          <el-tag v-else-if="scope.row.status === 3" type="warning"
            >审核失败</el-tag
          >
            <el-tag v-else-if="scope.row.status === 4" type="danger"
            >已删除</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="时间"> </el-table-column>
        <el-table-column prop="" label="操作">
          <template v-slot="scope">
              <el-button
                size="mini"
                icon="el-icon-edit"
                type="primary"
                circle
                @click="hEdit(scope.$index, scope.row)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                circle
                @click="hDelete(scope.$index, scope.row)"
              ></el-button>
        </template> </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @current-change='changePage'
        layout="prev, pager, next"
        :total="total"
        :current-page  = 'form.page'
        :disabled="load">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { channelListAPI, articleListAPI, articleDelAPI } from '@/api'
// console.log(articleListAPI)
export default {
  name: 'articleList',
  data () {
    return {
      form: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1, // 页数
        per_page: 10 // ，每页数量
      },
      channelList: [], // 存频道数据的
      tableData: [], // 表格数据
      total: 0, // 文章总数
      load: false // 实现加载loading
    }
  },
  methods: {
    searchFn () {
      this.form.page = 1
      this.articleListFn()
    },
    hEdit (index, obj) {
      // 跳转到编辑页面-发布跟编辑是一个页面
      this.$router.push({
        path: '/layout/addArticle',
        query: {
          id: obj.id
        }
      })
    },
    hDelete (index, obj) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const [err] = await articleDelAPI(obj.id)
        if (err) return
        this.articleListFn()
        // console.log(res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    changePage (page) {
      // console.log(page)
      this.form.page = page
      this.articleListFn()
    },
    async articleListFn () {
      this.load = true
      const [err, articleRes] = await articleListAPI(this.form)
      // console.log(articleRes)
      if (!err) {
        this.tableData = articleRes.data.data.results
        this.total = articleRes.data.data.total_count
      } this.load = false
    }

  },
  async created () {
    const [err, res] = await channelListAPI()
    // console.log(res)
    if (!err) {
      this.channelList = res.data.data.channels
      // 请求数据  调用封装的方法
      this.articleListFn()
    }
  }
}
</script>

<style scoped lang='less'>
.el-col-2 {
  text-align: center;
}
</style>
